﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Designer;
using FlexCell;
using YArchitech.Controls;
using YArchitech.LIB;

namespace HyCommonTableTool
{
	public partial class DlgHyCommonTableTool : System.Windows.Forms.Form
	{
		public DlgHyCommonTableTool(bool bEmpty)
		{
			this.InitializeComponent();
			this.cbtnBackColor.Value = ColorTranslator.ToWin32(Color.White);
			this.toolTip1.SetToolTip(this.cbtnBackColor, "背景色");
			this.cbtnFontColor.Value = ColorTranslator.ToWin32(Color.Black);
			this.toolTip1.SetToolTip(this.cbtnFontColor, "文字颜色");
			this.toolTip1.SetToolTip(this.abtnAlign, "对齐方式");
			this.toolTip1.SetToolTip(this.bbtnBorder, "边框样式");
			this.MenuUnFreeze.Enabled = false;
			this.tsbZoom.ComboBox.Items.Add("10%");
			this.tsbZoom.ComboBox.Items.Add("20%");
			this.tsbZoom.ComboBox.Items.Add("30%");
			this.tsbZoom.ComboBox.Items.Add("40%");
			this.tsbZoom.ComboBox.Items.Add("50%");
			this.tsbZoom.ComboBox.Items.Add("60%");
			this.tsbZoom.ComboBox.Items.Add("70%");
			this.tsbZoom.ComboBox.Items.Add("80%");
			this.tsbZoom.ComboBox.Items.Add("90%");
			this.tsbZoom.ComboBox.Items.Add("100%");
			this.tsbZoom.ComboBox.Items.Add("110%");
			this.tsbZoom.ComboBox.Items.Add("120%");
			this.tsbZoom.ComboBox.Items.Add("130%");
			this.tsbZoom.ComboBox.Items.Add("140%");
			this.tsbZoom.ComboBox.Items.Add("150%");
			this.tsbZoom.ComboBox.Items.Add("160%");
			this.tsbZoom.ComboBox.Items.Add("170%");
			this.tsbZoom.ComboBox.Items.Add("180%");
			this.tsbZoom.ComboBox.Items.Add("190%");
			this.tsbZoom.ComboBox.Items.Add("200%");
			this.tsbZoom.ComboBox.Items.Add("250%");
			this.tsbZoom.ComboBox.Items.Add("400%");
			this.tsbZoom.Text = "100%";
			foreach (FontFamily fontFamily in FontFamily.Families)
			{
				this.tsbFont.ComboBox.Items.Add(fontFamily.Name);
			}
			this.tsbFont.Text = "宋体";
			this.tsbFontSize.ComboBox.Items.Add("6");
			this.tsbFontSize.ComboBox.Items.Add("8");
			this.tsbFontSize.ComboBox.Items.Add("9");
			this.tsbFontSize.ComboBox.Items.Add("10");
			this.tsbFontSize.ComboBox.Items.Add("11");
			this.tsbFontSize.ComboBox.Items.Add("12");
			this.tsbFontSize.ComboBox.Items.Add("14");
			this.tsbFontSize.ComboBox.Items.Add("16");
			this.tsbFontSize.ComboBox.Items.Add("18");
			this.tsbFontSize.ComboBox.Items.Add("20");
			this.tsbFontSize.ComboBox.Items.Add("22");
			this.tsbFontSize.ComboBox.Items.Add("24");
			this.tsbFontSize.ComboBox.Items.Add("26");
			this.tsbFontSize.ComboBox.Items.Add("28");
			this.tsbFontSize.ComboBox.Items.Add("36");
			this.tsbFontSize.ComboBox.Items.Add("48");
			this.tsbFontSize.ComboBox.Items.Add("72");
			this.tsbFontSize.Text = "9";
			if (bEmpty)
			{
				this.grid1.Size = new Size(base.ClientSize.Width, base.ClientSize.Height - this.statusStrip1.Height - this.btnCancel.Size.Height - this.grid1.Top - 10);
				this.grid1.DisplayRowNumber = true;
				this.grid1.ShowResizeTip = true;
				this.grid1.Rows = 50;
				this.grid1.Cols = 50;
				this.grid1.ScrollBars = ScrollBarsEnum.Both;
				this.grid1.Cell(1, 1).SetFocus();
				this.grid1.Images.Clear();
			}
			this.DisplayColoumNum();
			this.btnCancel.Left = base.ClientSize.Width - this.btnCancel.Size.Width - 20;
			this.btnCancel.Top = base.ClientSize.Height - this.statusStrip1.Size.Height - this.btnCancel.Size.Height - 5;
			this.btnDrawTable.Left = base.ClientSize.Width - this.btnDrawTable.Size.Width - this.btnCancel.Size.Width - 40;
			this.btnDrawTable.Top = base.ClientSize.Height - this.statusStrip1.Size.Height - this.btnDrawTable.Size.Height - 5;
			this.tsslDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
			this.tsslRC.Text = "R" + this.grid1.ActiveCell.Row.ToString() + ":C" + this.grid1.ActiveCell.Col.ToString();
			this.DlgHyCommonTableTool_Resize(this, new EventArgs());
		}

		private void DisplayColoumNum()
		{
			if (this.grid1.Cols == 1)
			{
				return;
			}
			int cols = this.grid1.Cols;
			for (int i = 1; i < cols; i++)
			{
				this.grid1.Cell(0, i).Text = i.ToString();
			}
			AlignmentEnum alignment = AlignmentEnum.LeftTop;
			switch (this.abtnAlign.mintSelectedItem)
			{
			case 0:
				alignment = AlignmentEnum.LeftTop;
				break;
			case 1:
				alignment = AlignmentEnum.CenterTop;
				break;
			case 2:
				alignment = AlignmentEnum.RightTop;
				break;
			case 3:
				alignment = AlignmentEnum.LeftCenter;
				break;
			case 4:
				alignment = AlignmentEnum.CenterCenter;
				break;
			case 5:
				alignment = AlignmentEnum.RightCenter;
				break;
			case 6:
				alignment = AlignmentEnum.LeftBottom;
				break;
			case 7:
				alignment = AlignmentEnum.CenterBottom;
				break;
			case 8:
				alignment = AlignmentEnum.RightBottom;
				break;
			}
			this.grid1.Range(0, 0, 0, this.grid1.Cols - 1).Alignment = AlignmentEnum.CenterCenter;
			this.grid1.Range(1, 1, this.grid1.Rows - 1, this.grid1.Cols - 1).Alignment = alignment;
		}

		private void btnDrawTable_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private void DlgHyCommonTableTool_Resize(object sender, EventArgs e)
		{
			this.grid1.Top = this.menuStrip1.Size.Height + this.toolStrip1.Size.Height * Convert.ToInt32(this.MenuToolBar.Checked) * 2;
			int num = base.ClientSize.Height - this.statusStrip1.Height * Convert.ToInt32(this.MenuStatus.Checked) - this.btnCancel.Size.Height - this.grid1.Top - 10;
			if (num < 0)
			{
				num = 0;
			}
			this.grid1.Size = new Size(base.ClientSize.Width, num);
			this.btnCancel.Left = base.ClientSize.Width - this.btnCancel.Size.Width - 20;
			this.btnCancel.Top = base.ClientSize.Height - this.statusStrip1.Size.Height * Convert.ToInt32(this.MenuStatus.Checked) - this.btnCancel.Size.Height - 5;
			this.btnDrawTable.Left = base.ClientSize.Width - this.btnDrawTable.Size.Width - this.btnCancel.Size.Width - 40;
			this.btnDrawTable.Top = base.ClientSize.Height - this.statusStrip1.Size.Height * Convert.ToInt32(this.MenuStatus.Checked) - this.btnDrawTable.Size.Height - 5;
		}

		private void MenuQuit_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void grid1_RowColChange(object Sender, Grid.RowColChangeEventArgs e)
		{
			this.tsbFont.Text = this.grid1.Cell(e.Row, e.Col).Font.Name;
			this.tsbFontSize.Text = this.grid1.Cell(e.Row, e.Col).Font.Size.ToString();
			this.SetAlignment(this.grid1.Cell(e.Row, e.Col).Alignment);
			this.tsslRC.Text = "R" + this.grid1.ActiveCell.Row.ToString() + ":C" + this.grid1.ActiveCell.Col.ToString();
		}

		private void SetAlignment(AlignmentEnum alignment)
		{
			switch (alignment)
			{
			case AlignmentEnum.LeftTop:
				this.abtnAlign.mintSelectedItem = 0;
				break;
			case AlignmentEnum.LeftCenter:
				this.abtnAlign.mintSelectedItem = 3;
				break;
			case AlignmentEnum.LeftBottom:
				this.abtnAlign.mintSelectedItem = 6;
				break;
			case AlignmentEnum.CenterTop:
				this.abtnAlign.mintSelectedItem = 1;
				break;
			case AlignmentEnum.CenterCenter:
				this.abtnAlign.mintSelectedItem = 4;
				break;
			case AlignmentEnum.CenterBottom:
				this.abtnAlign.mintSelectedItem = 7;
				break;
			case AlignmentEnum.RightTop:
				this.abtnAlign.mintSelectedItem = 2;
				break;
			case AlignmentEnum.RightCenter:
				this.abtnAlign.mintSelectedItem = 5;
				break;
			case AlignmentEnum.RightBottom:
				this.abtnAlign.mintSelectedItem = 8;
				break;
			}
			this.abtnAlign.Refresh();
		}

		private void MenuNew_Click(object sender, EventArgs e)
		{
			this.grid1.NewFile();
			this.grid1.AutoRedraw = false;
			this.grid1.DisplayRowNumber = true;
			this.grid1.ShowResizeTip = true;
			this.grid1.Rows = 50;
			this.grid1.Cols = 50;
			this.grid1.AutoRedraw = true;
			this.grid1.Refresh();
			this.m_strFilePath = "";
			this.DisplayColoumNum();
		}

		private void tsbNew_Click(object sender, EventArgs e)
		{
			if (YJKMessageBox.Show("是否保存当前文档", MessageBoxButtons.OKCancel) == DialogResult.OK)
			{
				this.MenuSave_Click(this, new EventArgs());
			}
			this.MenuNew_Click(this, new EventArgs());
		}

		private void MenuOpen_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "Excel文件|*.xls|Excel文件|*.xlsx|盈建科表格文件|*.hybg";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.m_strFilePath = openFileDialog.FileName;
				this.m_eFileType = (DlgHyCommonTableTool.FileType)openFileDialog.FilterIndex;
				switch (openFileDialog.FilterIndex)
				{
				case 1:
					new GridToolExportImportExcel(false).ImportExcel(ref this.grid1, openFileDialog.FileName);
					this.grid1.Refresh();
					break;
				case 2:
					new GridToolExportImportExcel(true).ImportExcel(ref this.grid1, openFileDialog.FileName);
					this.grid1.Refresh();
					break;
				case 3:
					this.grid1.OpenFile(openFileDialog.FileName);
					break;
				}
				this.grid1.BackColorBkg = Color.White;
			}
		}

		private void tsbOpen_Click(object sender, EventArgs e)
		{
			this.MenuOpen_Click(this, new EventArgs());
		}

		private void MenuSave_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.m_strFilePath))
			{
				SaveFileDialog saveFileDialog = new SaveFileDialog();
				saveFileDialog.Filter = "Excel文件|*.xls|Excel文件|*.xlsx|盈建科表格文件|*.hybg";
				if (saveFileDialog.ShowDialog() == DialogResult.OK)
				{
					this.m_strFilePath = saveFileDialog.FileName;
					this.m_eFileType = (DlgHyCommonTableTool.FileType)saveFileDialog.FilterIndex;
				}
			}
			bool flag = false;
			switch (this.m_eFileType)
			{
			case DlgHyCommonTableTool.FileType.XLS:
				flag = new GridToolExportImportExcel(false).ExportExcel(this.grid1, this.m_strFilePath);
				break;
			case DlgHyCommonTableTool.FileType.XLSX:
				flag = new GridToolExportImportExcel(true).ExportExcel(this.grid1, this.m_strFilePath);
				break;
			case DlgHyCommonTableTool.FileType.HYBG:
				flag = this.grid1.SaveFile(this.m_strFilePath);
				break;
			}
			if (flag)
			{
				YJKMessageBox.Show("文件保存成功");
			}
		}

		private void tsbSave_Click(object sender, EventArgs e)
		{
			this.MenuSave_Click(this, new EventArgs());
		}

		private void MenuSaveAs_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.Filter = "Excel文件|*.xls|Excel文件|*.xlsx|盈建科表格文件|*.hybg";
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.m_strFilePath = saveFileDialog.FileName;
				this.m_eFileType = (DlgHyCommonTableTool.FileType)saveFileDialog.FilterIndex;
				bool flag = false;
				switch (saveFileDialog.FilterIndex)
				{
				case 1:
					flag = new GridToolExportImportExcel(false).ExportExcel(this.grid1, this.m_strFilePath);
					break;
				case 2:
					flag = new GridToolExportImportExcel(true).ExportExcel(this.grid1, this.m_strFilePath);
					break;
				case 3:
					flag = this.grid1.SaveFile(this.m_strFilePath);
					break;
				}
				if (flag)
				{
					YJKMessageBox.Show("文件保存成功");
				}
			}
		}

		private void tsbSaveAs_Click(object sender, EventArgs e)
		{
			this.MenuSaveAs_Click(this, new EventArgs());
		}

		private void MenuQuitCurrent_Click(object sender, EventArgs e)
		{
			if (YJKMessageBox.Show("是否保存当前文档", MessageBoxButtons.OKCancel) == DialogResult.OK)
			{
				this.MenuSave_Click(this, new EventArgs());
			}
			this.MenuNew_Click(this, new EventArgs());
		}

		private void tsbQuitCurrent_Click(object sender, EventArgs e)
		{
			this.MenuQuitCurrent_Click(this, new EventArgs());
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void MenuCut_Click(object sender, EventArgs e)
		{
			SendKeys.Send("^X");
		}

		private void tsbCut_Click(object sender, EventArgs e)
		{
			this.MenuCut_Click(this, new EventArgs());
		}

		private void MenuCopy_Click(object sender, EventArgs e)
		{
			SendKeys.Send("^C");
		}

		private void tsbCopy_Click(object sender, EventArgs e)
		{
			this.MenuCopy_Click(this, new EventArgs());
		}

		private void MenuPaste_Click(object sender, EventArgs e)
		{
			SendKeys.Send("^V");
		}

		private void tsbPaste_Click(object sender, EventArgs e)
		{
			this.MenuPaste_Click(this, new EventArgs());
		}

		private void MenuClearAll_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.ClearAll();
		}

		private void MenuClearContent_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.ClearText();
		}

		private void MenuClearFormat_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.ClearFormat();
		}

		private void MenuClearBackColor_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.ClearBackColor();
		}

		private void MenuInsertRow_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.InsertRows();
		}

		private void tsbInsertRow_Click(object sender, EventArgs e)
		{
			this.MenuInsertRow_Click(this, new EventArgs());
		}

		private void MenuInsertCol_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.InsertCols();
			this.DisplayColoumNum();
		}

		private void tsbInsertCol_Click(object sender, EventArgs e)
		{
			this.MenuInsertCol_Click(this, new EventArgs());
		}

		private void MenuDeleteRow_Click(object sender, EventArgs e)
		{
			if (this.grid1.Selection.SelectedObject == SelectedObjectEnum.EntireRow)
			{
				this.grid1.Selection.DeleteByRow();
			}
		}

		private void tsbDeleteRow_Click(object sender, EventArgs e)
		{
			this.MenuDeleteRow_Click(this, new EventArgs());
		}

		private void MenuDeleteCol_Click(object sender, EventArgs e)
		{
			if (this.grid1.Selection.SelectedObject == SelectedObjectEnum.EntireColumn)
			{
				this.grid1.Selection.DeleteByCol();
				this.DisplayColoumNum();
			}
		}

		private void tsbDeleteCol_Click(object sender, EventArgs e)
		{
			this.MenuDeleteCol_Click(this, new EventArgs());
		}

		private void MenuMerge_Click(object sender, EventArgs e)
		{
			int firstRow = this.grid1.Selection.FirstRow;
			int lastRow = this.grid1.Selection.LastRow;
			int firstCol = this.grid1.Selection.FirstCol;
			int lastCol = this.grid1.Selection.LastCol;
			int num = 0;
			for (int i = firstRow; i <= lastRow; i++)
			{
				for (int j = firstCol; j <= lastCol; j++)
				{
					if (this.grid1.Cell(i, j).Text != "")
					{
						num++;
					}
				}
			}
			if (num > 1 && YJKMessageBox.Show("合并到一个单元格后只能保留最左上角的数据", MessageBoxButtons.OKCancel) != DialogResult.OK)
			{
				return;
			}
			try
			{
				this.grid1.Selection.Merge();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.Message);
			}
		}

		private void tsbMerge_Click(object sender, EventArgs e)
		{
			this.MenuMerge_Click(this, new EventArgs());
		}

		private void MenuUnMerge_Click(object sender, EventArgs e)
		{
			try
			{
				this.grid1.Selection.MergeCells = false;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.Message);
			}
		}

		private void tsbUnMerge_Click(object sender, EventArgs e)
		{
			this.MenuUnMerge_Click(this, new EventArgs());
		}

		private void MenuNewLine_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.WrapText = true;
		}

		private void tsbNewLine_Click(object sender, EventArgs e)
		{
			this.MenuNewLine_Click(this, new EventArgs());
		}

		private void MenuOneLine_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.WrapText = false;
		}

		private void MenuImage_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Title = "打开";
			openFileDialog.Filter = "图片文件|*.bmp;*.gif;*.jpg;*.jpeg;*.png;*.icon;*.tif;*.tiff";
			openFileDialog.FileName = "";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				string fileName = openFileDialog.FileName;
				if (File.Exists(fileName))
				{
					string extension = Path.GetExtension(fileName);
					string text = this.grid1.Images.Count().ToString() + extension;
					this.grid1.Images.Add(fileName, text);
					this.grid1.ActiveCell.SetImage(text);
					this.grid1.ActiveCell.Alignment = AlignmentEnum.CenterCenter;
					this.grid1.Refresh();
				}
			}
			openFileDialog.Dispose();
		}

		private void tsbImage_Click(object sender, EventArgs e)
		{
			this.MenuImage_Click(this, new EventArgs());
		}

		private void MenuImageH_Click(object sender, EventArgs e)
		{
			for (int i = this.grid1.Selection.FirstRow; i <= this.grid1.Selection.LastRow; i++)
			{
				for (int j = this.grid1.Selection.FirstCol; j <= this.grid1.Selection.LastCol; j++)
				{
					if (this.grid1.Cell(i, j).ImageKey != "")
					{
						switch (this.grid1.Cell(i, j).Alignment)
						{
						case AlignmentEnum.LeftGeneral:
						case AlignmentEnum.CenterGeneral:
						case AlignmentEnum.RightGeneral:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.GeneralGeneral;
							break;
						case AlignmentEnum.LeftTop:
						case AlignmentEnum.CenterTop:
						case AlignmentEnum.RightTop:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.GeneralTop;
							break;
						case AlignmentEnum.LeftCenter:
						case AlignmentEnum.CenterCenter:
						case AlignmentEnum.RightCenter:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.GeneralCenter;
							break;
						case AlignmentEnum.LeftBottom:
						case AlignmentEnum.CenterBottom:
						case AlignmentEnum.RightBottom:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.GeneralBottom;
							break;
						}
					}
				}
			}
		}

		private void MenuImageV_Click(object sender, EventArgs e)
		{
			for (int i = this.grid1.Selection.FirstRow; i <= this.grid1.Selection.LastRow; i++)
			{
				for (int j = this.grid1.Selection.FirstCol; j <= this.grid1.Selection.LastCol; j++)
				{
					if (this.grid1.Cell(i, j).ImageKey != "")
					{
						switch (this.grid1.Cell(i, j).Alignment)
						{
						case AlignmentEnum.GeneralTop:
						case AlignmentEnum.GeneralCenter:
						case AlignmentEnum.GeneralBottom:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.GeneralGeneral;
							break;
						case AlignmentEnum.LeftTop:
						case AlignmentEnum.LeftCenter:
						case AlignmentEnum.LeftBottom:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.LeftGeneral;
							break;
						case AlignmentEnum.CenterTop:
						case AlignmentEnum.CenterCenter:
						case AlignmentEnum.CenterBottom:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.CenterGeneral;
							break;
						case AlignmentEnum.RightTop:
						case AlignmentEnum.RightCenter:
						case AlignmentEnum.RightBottom:
							this.grid1.Cell(i, j).Alignment = AlignmentEnum.RightGeneral;
							break;
						}
					}
				}
			}
		}

		private void MenuImageHV_Click(object sender, EventArgs e)
		{
			for (int i = this.grid1.Selection.FirstRow; i <= this.grid1.Selection.LastRow; i++)
			{
				for (int j = this.grid1.Selection.FirstCol; j <= this.grid1.Selection.LastCol; j++)
				{
					if (this.grid1.Cell(i, j).ImageKey != "")
					{
						this.grid1.Cell(i, j).Alignment = AlignmentEnum.GeneralGeneral;
					}
				}
			}
		}

		private void MenuImageNone_Click(object sender, EventArgs e)
		{
			for (int i = this.grid1.Selection.FirstRow; i <= this.grid1.Selection.LastRow; i++)
			{
				for (int j = this.grid1.Selection.FirstCol; j <= this.grid1.Selection.LastCol; j++)
				{
					if (this.grid1.Cell(i, j).ImageKey != "")
					{
						this.grid1.Cell(i, j).Alignment = AlignmentEnum.CenterCenter;
					}
				}
			}
		}

		private void MenuToolBar_Click(object sender, EventArgs e)
		{
			if (this.MenuToolBar.CheckState == CheckState.Checked)
			{
				this.MenuToolBar.CheckState = CheckState.Unchecked;
				this.toolStrip1.Visible = false;
				this.toolStrip2.Visible = false;
				this.cbtnBackColor.Visible = false;
				this.cbtnFontColor.Visible = false;
				this.abtnAlign.Visible = false;
				this.bbtnBorder.Visible = false;
			}
			else
			{
				this.MenuToolBar.CheckState = CheckState.Checked;
				this.toolStrip1.Visible = true;
				this.toolStrip2.Visible = true;
				this.cbtnBackColor.Visible = true;
				this.cbtnFontColor.Visible = true;
				this.abtnAlign.Visible = true;
				this.bbtnBorder.Visible = true;
			}
			this.DlgHyCommonTableTool_Resize(this, new EventArgs());
		}

		private void MenuGridLine_Click(object sender, EventArgs e)
		{
			if (this.MenuGridLine.CheckState == CheckState.Checked)
			{
				this.MenuGridLine.CheckState = CheckState.Unchecked;
				this.grid1.GridColor = Color.White;
				this.grid1.HideGridLines = true;
				return;
			}
			this.MenuGridLine.CheckState = CheckState.Checked;
			this.grid1.HideGridLines = false;
			this.grid1.GridColor = Color.Silver;
		}

		private void MenuHScrollBar_Click(object sender, EventArgs e)
		{
			if (this.MenuHScrollBar.CheckState == CheckState.Checked)
			{
				this.MenuHScrollBar.CheckState = CheckState.Unchecked;
				if (this.grid1.VScrollbarVisible)
				{
					this.grid1.ScrollBars = ScrollBarsEnum.Vertical;
					return;
				}
				this.grid1.ScrollBars = ScrollBarsEnum.None;
				return;
			}
			else
			{
				this.MenuHScrollBar.CheckState = CheckState.Checked;
				if (this.grid1.VScrollbarVisible)
				{
					this.grid1.ScrollBars = ScrollBarsEnum.Both;
					return;
				}
				this.grid1.ScrollBars = ScrollBarsEnum.Horizontal;
				return;
			}
		}

		private void MenuVScrollBar_Click(object sender, EventArgs e)
		{
			if (this.MenuVScrollBar.CheckState == CheckState.Checked)
			{
				this.MenuVScrollBar.CheckState = CheckState.Unchecked;
				if (this.grid1.HScrollbarVisible)
				{
					this.grid1.ScrollBars = ScrollBarsEnum.Horizontal;
					return;
				}
				this.grid1.ScrollBars = ScrollBarsEnum.None;
				return;
			}
			else
			{
				this.MenuVScrollBar.CheckState = CheckState.Checked;
				if (this.grid1.HScrollbarVisible)
				{
					this.grid1.ScrollBars = ScrollBarsEnum.Both;
					return;
				}
				this.grid1.ScrollBars = ScrollBarsEnum.Vertical;
				return;
			}
		}

		private void MenuStatus_Click(object sender, EventArgs e)
		{
			if (this.MenuStatus.CheckState == CheckState.Checked)
			{
				this.MenuStatus.CheckState = CheckState.Unchecked;
				this.statusStrip1.Visible = false;
			}
			else
			{
				this.MenuStatus.CheckState = CheckState.Checked;
				this.statusStrip1.Visible = true;
			}
			this.DlgHyCommonTableTool_Resize(this, new EventArgs());
		}

		private void cbtnBackColor_Click(object Sender, EventArgs e)
		{
			this.grid1.Selection.BackColor = ColorTranslator.FromWin32(this.cbtnBackColor.Value);
		}

		private void cbtnFontColor_Click(object Sender, EventArgs e)
		{
			this.grid1.Selection.ForeColor = ColorTranslator.FromWin32(this.cbtnFontColor.Value);
		}

		private void tsbBold_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.FontBold = !this.grid1.ActiveCell.Font.Bold;
		}

		private void tsbItalic_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.FontItalic = !this.grid1.ActiveCell.Font.Italic;
		}

		private void tsbUnderLine_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.FontUnderline = !this.grid1.ActiveCell.Font.Underline;
		}

		private void tsbDeleFont_Click(object sender, EventArgs e)
		{
			this.grid1.Selection.FontStrikeout = !this.grid1.ActiveCell.Font.Strikeout;
		}

		private void tsbHelp_Click(object sender, EventArgs e)
		{
		}

		private void MenuFreeze_Click(object sender, EventArgs e)
		{
			Cell activeCell = this.grid1.ActiveCell;
			int num = activeCell.Row;
			int num2 = activeCell.Col;
			if (activeCell.MergeCell)
			{
				Range range = this.grid1.Range(num, num2, num, num2);
				num = range.LastRow;
				num2 = range.LastCol;
			}
			this.grid1.FrozenRows = num;
			this.grid1.FrozenCols = num2;
			this.MenuUnFreeze.Enabled = true;
		}

		private void MenuUnFreeze_Click(object sender, EventArgs e)
		{
			this.grid1.FrozenRows = 0;
			this.grid1.FrozenCols = 0;
			this.MenuUnFreeze.Enabled = false;
		}

		private void abtnAlign_Click(object Sender, AlignButton.ClickEventArgs e)
		{
			if (this.grid1.ActiveCell.ImageKey != "")
			{
				return;
			}
			switch (e.Value)
			{
			case 0:
				this.grid1.Selection.Alignment = AlignmentEnum.LeftTop;
				return;
			case 1:
				this.grid1.Selection.Alignment = AlignmentEnum.CenterTop;
				return;
			case 2:
				this.grid1.Selection.Alignment = AlignmentEnum.RightTop;
				return;
			case 3:
				this.grid1.Selection.Alignment = AlignmentEnum.LeftCenter;
				return;
			case 4:
				this.grid1.Selection.Alignment = AlignmentEnum.CenterCenter;
				return;
			case 5:
				this.grid1.Selection.Alignment = AlignmentEnum.RightCenter;
				return;
			case 6:
				this.grid1.Selection.Alignment = AlignmentEnum.LeftBottom;
				return;
			case 7:
				this.grid1.Selection.Alignment = AlignmentEnum.CenterBottom;
				return;
			case 8:
				this.grid1.Selection.Alignment = AlignmentEnum.RightBottom;
				return;
			default:
				return;
			}
		}

		private void bbtnBorder_Click(object Sender, BorderButton.ClickEventArgs e)
		{
			switch (e.Value)
			{
			case 0:
				this.grid1.Selection.set_Borders((EdgeEnum)255, LineStyleEnum.None);
				return;
			case 1:
				this.grid1.Selection.set_Borders(EdgeEnum.Top, LineStyleEnum.Thin);
				return;
			case 2:
				this.grid1.Selection.set_Borders(EdgeEnum.Bottom, LineStyleEnum.Thin);
				return;
			case 3:
				this.grid1.Selection.set_Borders(EdgeEnum.Left, LineStyleEnum.Thin);
				return;
			case 4:
				this.grid1.Selection.set_Borders(EdgeEnum.Right, LineStyleEnum.Thin);
				return;
			case 5:
				this.grid1.Selection.set_Borders(EdgeEnum.DiagonalUp, LineStyleEnum.Thin);
				return;
			case 6:
				this.grid1.Selection.set_Borders(EdgeEnum.DiagonalDown, LineStyleEnum.Thin);
				return;
			case 7:
				this.grid1.Selection.set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
				return;
			case 8:
				this.grid1.Selection.set_Borders(EdgeEnum.Outside, LineStyleEnum.Thin);
				return;
			case 9:
				this.grid1.Selection.set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
				return;
			case 10:
				this.grid1.Selection.set_Borders((EdgeEnum)207, LineStyleEnum.Thin);
				return;
			case 11:
				this.grid1.AutoRedraw = false;
				this.grid1.Selection.set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
				this.grid1.Selection.set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
				this.grid1.AutoRedraw = true;
				this.grid1.Refresh();
				return;
			default:
				return;
			}
		}

		private void tsbFont_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.grid1.Selection.FontName = this.tsbFont.Text;
				this.grid1.Refresh();
			}
			catch
			{
			}
		}

		private void tsbFontSize_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.grid1.Selection.FontSize = (float)int.Parse(this.tsbFontSize.Text);
			this.grid1.Refresh();
		}

		private void tsbZoom_SelectedIndexChanged(object sender, EventArgs e)
		{
			int num = (int)short.Parse(this.tsbZoom.Text.TrimEnd(new char[]
			{
				'%'
			}));
			this.grid1.DefaultRowHeight = Convert.ToInt16((double)(num * 19) * 0.01);
			for (int i = 1; i < this.grid1.Cols; i++)
			{
				this.grid1.Column(i).Width = Convert.ToInt16((double)(num * 80) * 0.01);
			}
			this.grid1.Refresh();
		}

		private void tsbCout_Click(object sender, EventArgs e)
		{
			int firstRow = this.grid1.Selection.FirstRow;
			int lastRow = this.grid1.Selection.LastRow;
			int firstCol = this.grid1.Selection.FirstCol;
			int lastCol = this.grid1.Selection.LastCol;
			for (int i = firstCol; i <= lastCol; i++)
			{
				int num = 0;
				for (int j = firstRow; j <= lastRow; j++)
				{
					if (this.grid1.Cell(j, i).Text != "")
					{
						num++;
					}
				}
				if (this.grid1.Rows - 1 == lastRow)
				{
					this.grid1.Rows = this.grid1.Rows + 1;
					for (int k = 1; k < this.grid1.Cols; k++)
					{
						this.grid1.Cell(this.grid1.Rows - 1, k).Alignment = this.grid1.Column(k).Alignment;
					}
				}
				this.grid1.Cell(lastRow + 1, i).Text = num.ToString();
			}
		}

		private void tsbSum_Click(object sender, EventArgs e)
		{
			int firstRow = this.grid1.Selection.FirstRow;
			int lastRow = this.grid1.Selection.LastRow;
			int firstCol = this.grid1.Selection.FirstCol;
			int lastCol = this.grid1.Selection.LastCol;
			for (int i = firstCol; i <= lastCol; i++)
			{
				double num = 0.0;
				for (int j = firstRow; j <= lastRow; j++)
				{
					string text = this.grid1.Cell(j, i).Text;
					if (text != "")
					{
						try
						{
							num += Convert.ToDouble(text);
						}
						catch
						{
							num += 0.0;
						}
					}
				}
				if (this.grid1.Rows - 1 == lastRow)
				{
					this.grid1.Rows = this.grid1.Rows + 1;
				}
				string text2 = num.ToString();
				if (text2.IndexOf('.') != -1)
				{
					text2 = text2.TrimEnd(new char[]
					{
						'0'
					});
					text2 = text2.Trim(new char[]
					{
						'.'
					});
				}
				this.grid1.Cell(lastRow + 1, i).Text = text2;
			}
		}

		private void tsbAverage_Click(object sender, EventArgs e)
		{
			int firstRow = this.grid1.Selection.FirstRow;
			int lastRow = this.grid1.Selection.LastRow;
			int firstCol = this.grid1.Selection.FirstCol;
			int lastCol = this.grid1.Selection.LastCol;
			for (int i = firstCol; i <= lastCol; i++)
			{
				double num = 0.0;
				for (int j = firstRow; j <= lastRow; j++)
				{
					string text = this.grid1.Cell(j, i).Text;
					if (text != "")
					{
						try
						{
							num += Convert.ToDouble(text);
						}
						catch
						{
							num += 0.0;
						}
					}
				}
				if (this.grid1.Rows - 1 == lastRow)
				{
					this.grid1.Rows = this.grid1.Rows + 1;
				}
				num /= (double)(lastRow - firstRow + 1);
				string text2 = num.ToString("0.00");
				if (text2.IndexOf('.') != -1)
				{
					text2 = text2.TrimEnd(new char[]
					{
						'0'
					});
					text2 = text2.Trim(new char[]
					{
						'.'
					});
				}
				this.grid1.Cell(lastRow + 1, i).Text = text2;
			}
		}

		private void tsbMax_Click(object sender, EventArgs e)
		{
			int firstRow = this.grid1.Selection.FirstRow;
			int lastRow = this.grid1.Selection.LastRow;
			int firstCol = this.grid1.Selection.FirstCol;
			int lastCol = this.grid1.Selection.LastCol;
			for (int i = firstCol; i <= lastCol; i++)
			{
				double num = double.MinValue;
				for (int j = firstRow; j <= lastRow; j++)
				{
					string text = this.grid1.Cell(j, i).Text;
					if (text != "")
					{
						try
						{
							double num2 = Convert.ToDouble(text);
							if (num2 - num > 0.001)
							{
								num = num2;
							}
						}
						catch
						{
						}
					}
				}
				if (this.grid1.Rows - 1 == lastRow)
				{
					this.grid1.Rows = this.grid1.Rows + 1;
				}
				string text2;
				if (num.Equals(-1.7976931348623157E+308))
				{
					text2 = "0";
				}
				else
				{
					text2 = num.ToString();
				}
				if (text2.IndexOf('.') != -1)
				{
					text2 = text2.TrimEnd(new char[]
					{
						'0'
					});
					text2 = text2.Trim(new char[]
					{
						'.'
					});
				}
				this.grid1.Cell(lastRow + 1, i).Text = text2;
			}
		}

		private void tsbMin_Click(object sender, EventArgs e)
		{
			int firstRow = this.grid1.Selection.FirstRow;
			int lastRow = this.grid1.Selection.LastRow;
			int firstCol = this.grid1.Selection.FirstCol;
			int lastCol = this.grid1.Selection.LastCol;
			for (int i = firstCol; i <= lastCol; i++)
			{
				double num = double.MaxValue;
				for (int j = firstRow; j <= lastRow; j++)
				{
					string text = this.grid1.Cell(j, i).Text;
					if (text != "")
					{
						try
						{
							double num2 = Convert.ToDouble(text);
							if (num2 - num < 0.001)
							{
								num = num2;
							}
						}
						catch
						{
						}
					}
				}
				if (this.grid1.Rows - 1 == lastRow)
				{
					this.grid1.Rows = this.grid1.Rows + 1;
				}
				string text2;
				if (num.Equals(1.7976931348623157E+308))
				{
					text2 = "0";
				}
				else
				{
					text2 = num.ToString();
				}
				if (text2.IndexOf('.') != -1)
				{
					text2 = text2.TrimEnd(new char[]
					{
						'0'
					});
					text2 = text2.Trim(new char[]
					{
						'.'
					});
				}
				this.grid1.Cell(lastRow + 1, i).Text = text2;
			}
		}

		private void DlgHyCommonTableTool_Load(object sender, EventArgs e)
		{
            //base.Icon = Resource.YArchitechIcon;
		}

		private void MenuOpenTmp_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			int disciplineValue = Product.GetDisciplineValue();
			string initialDirectory = "";
			switch (disciplineValue)
			{
			case 1:
				initialDirectory = Path.Combine(Product.InstallLocation, "Templates", "StatisticTempletlFile");
				break;
			case 2:
				initialDirectory = Path.Combine(Product.InstallLocation, "Templates", "HvacTempletlFile");
				break;
			case 3:
				break;
			case 4:
				initialDirectory = Path.Combine(Product.InstallLocation, "Templates", "PlumbingTempletlFile");
				break;
			default:
				if (disciplineValue == 8)
				{
					initialDirectory = Path.Combine(Product.InstallLocation, "Templates", "ElectricTempletlFile");
				}
				break;
			}
			openFileDialog.InitialDirectory = initialDirectory;
			openFileDialog.Filter = "盈建科表格模板文件|*.hymb";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.m_strFilePath = openFileDialog.FileName;
				if (!this.grid1.LoadFromXML(this.m_strFilePath))
				{
					YJKMessageBox.Show("表格模板加载失败");
				}
				this.grid1.BackColorBkg = Color.White;
			}
		}

		private void tsbUp_Click(object sender, EventArgs e)
		{
			if (this.grid1.Selection.SelectedObject == SelectedObjectEnum.EntireRow)
			{
				int firstRow = this.grid1.Selection.FirstRow;
				int lastRow = this.grid1.Selection.LastRow;
				if (firstRow == 1)
				{
					return;
				}
				this.grid1.Selection.CopyData();
				this.grid1.Selection.DeleteByRow();
				this.grid1.InsertRow(firstRow - 1, lastRow - firstRow + 1);
				this.grid1.Range(firstRow - 1, 1, lastRow - 1, this.grid1.Cols - 1).PasteData();
				this.grid1.Range(firstRow - 1, 1, lastRow - 1, this.grid1.Cols - 1).SelectCells();
			}
		}

		private void tsbDown_Click(object sender, EventArgs e)
		{
			if (this.grid1.Selection.SelectedObject == SelectedObjectEnum.EntireRow)
			{
				int firstRow = this.grid1.Selection.FirstRow;
				int lastRow = this.grid1.Selection.LastRow;
				if (lastRow == this.grid1.Rows - 1)
				{
					return;
				}
				this.grid1.Selection.CopyData();
				this.grid1.Selection.DeleteByRow();
				if (this.grid1.Rows - 1 == firstRow)
				{
					this.grid1.Rows = this.grid1.Rows + lastRow - firstRow + 1;
				}
				else
				{
					this.grid1.InsertRow(firstRow + 1, lastRow - firstRow + 1);
				}
				this.grid1.Range(firstRow + 1, 1, lastRow + 1, this.grid1.Cols - 1).PasteData();
				this.grid1.Range(firstRow + 1, 1, lastRow + 1, this.grid1.Cols - 1).SelectCells();
			}
		}

		private string m_strFilePath = "";

		private DlgHyCommonTableTool.FileType m_eFileType;

		private const int m_nCellWidth = 80;

		private const int m_nCellHeight = 19;

		private enum FileType
		{
			XLS = 1,
			XLSX,
			HYBG
		}
	}
}
